package com.cuckoo.cuckoo_app2.business.Imp;

import android.content.Context;
import android.icu.text.SimpleDateFormat;
import android.util.Log;

import com.cuckoo.cuckoo_app2.MyApplication;
import com.cuckoo.cuckoo_app2.apiAsync.BaseCallbac;
import com.cuckoo.cuckoo_app2.apiAsync.Imp.UserApiImp;
import com.cuckoo.cuckoo_app2.apiAsync.UserApi;
import com.cuckoo.cuckoo_app2.business.UserBusiness;
import com.cuckoo.cuckoo_app2.dao.SysUser;
import com.cuckoo.cuckoo_app2.dao.User;
import com.cuckoo.cuckoo_app2.greendao.DaoSession;
import com.cuckoo.cuckoo_app2.utils.RouteUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;

import java.text.ParseException;
import java.util.Date;

public class UserBusinessImp implements UserBusiness {

    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private UserApi userApi;

    public UserBusinessImp() {
        this.userApi = new UserApiImp();
    }

    @Override
    public void saveLoginInfo(User user) {
        DaoSession db = MyApplication.getInstances().getDaoSession();
        db.getUserDao().deleteAll();
        db.getUserDao().insert(user);
    }

    @Override
    public void logout() {
        DaoSession db = MyApplication.getInstances().getDaoSession();
        db.getUserDao().deleteAll();
    }

    @Override
    public void updata(User user) {
        UserBusiness userBusiness = new UserBusinessImp();
        userBusiness.saveLoginInfo(user);
        MyApplication.getInstances().clearUser();
    }

    @Override
    public void updateUserInfoContext() {
        User user = MyApplication.getInstances().getUser();
        if (user == null || user.getId().isEmpty()) {
            return;
        }

        // 更新个人信息
        // 更新vip信息
        userApi.performGetUserInfoAsync(new BaseCallbac<JsonObject>() {

            @Override
            public void onSuccess(JsonObject data) {
                if (!data.get("success").getAsBoolean()) {
                    return;
                }
                JsonObject result = data.get("result").getAsJsonObject();
                // 用户信息
                Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
                SysUser sysUser = gson.fromJson(result.get("userInfo"), SysUser.class);
                JsonObject vipInfo = result.get("vipInfo").getAsJsonObject();
                String isVip = vipInfo.get("isVip").getAsString();
                String vipEndTime = vipInfo.get("endVip").getAsString();

                user.setId(sysUser.getId());
                user.setName(sysUser.getUsername());
                user.setPhone(sysUser.getPhone());
                user.setImgUrl(sysUser.getAvatar());
                user.setAddTime(sysUser.getCreateTime());
                user.setIsVip(Integer.parseInt(isVip));

                try {
                    Date date = dateFormat.parse(vipEndTime);
                    user.setEndVip(date);
                } catch (ParseException e) {
                    Log.e("TEST", e.getMessage());
                }

                // 更新
                updata(user);
            }

            @Override
            public void onFailure(String errorMessage) {
                Log.e("TEST", errorMessage);
                deleteAll();
                Context context = MyApplication.getInstances().getContext();
                // 发送登录失败重新登录
                RouteUtils.toLoginActivity(context);
            }
        });
    }

    @Override
    public void getVipInfo() {
        User user = MyApplication.getInstances().getUser();
        if (user == null || user.getId().isEmpty()) {
            return;
        }
        userApi.performGetVipInfoAsync(user.getId(), new BaseCallbac<JsonObject>() {
            @Override
            public void onSuccess(JsonObject data) {
                if (!data.get("success").getAsBoolean()) {
                    return;
                }
                JsonObject result = data.get("result").getAsJsonObject();

                String isVip = result.get("isVip").getAsString();
                Date EndTime = null;

                try {
                    String vipEndTime = result.get("endVip").getAsString();
                    EndTime = dateFormat.parse(vipEndTime);
                } catch (ParseException e) {
                    Log.e("TEST", e.getMessage());
                }

                if (user.getIsVip().equals(Integer.parseInt(isVip)) && user.getEndVip().equals(EndTime)) {
                    return;
                }

                user.setIsVip(Integer.parseInt(isVip));
                user.setEndVip(EndTime);

                // 更新
                updata(user);
            }

            @Override
            public void onFailure(String errorMessage) {
                deleteAll();
                Context context = MyApplication.getInstances().getContext();
                // 发送登录失败重新登录
                RouteUtils.toLoginActivity(context);
            }
        });
    }

    @Override
    public void deleteAll() {
        DaoSession db = MyApplication.getInstances().getDaoSession();
        db.getUserDao().deleteAll();
        MyApplication.getInstances().clearUser();
    }
}
